<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;
use Throwable;
use app\common\Backend;
use app\common\model\Combo;
use app\common\model\Device as ModelDevice;
use app\common\model\Navigation;
use app\common\model\Product as ModelProduct;
use app\common\model\PublicStatus;
use app\common\model\Service as ModelService;
use general\Tree;
use general\Random;
use Qiniu\Auth;
use think\Request;

class Service extends Backend
{

    /**
     * @var Service
     */
    protected $tree = null;

    protected $model = null;

    protected $quickSearchField = ['title'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new ModelService();
        $this->tree = Tree::instance();

    }

     /**
     * 数据源
     * @throws Throwable
     */
    public function index(): void
    {
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model->where($where)
            ->order($order)
            ->paginate($limit);

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => '',
        ]);
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
          
            if (!$data) {
                $this->error(('Parameter %s can not be empty'));
            }
            $data = $this->excludeFields($data);
            if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                $data[$this->dataLimitField] = $this->auth->id;
            }
            $data['url'] = reset($data['banner_path']);
            $data['banner_path'] = json_encode($data['banner_path'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            $result = false;
            $this->model->startTrans();
            try {
                $result = $this->model->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(('Added successfully'));
            } else {
                $this->error(('No rows were added'));
            }
        }
        $this->error(('Parameter error'));
    }



     /**
     * 编辑
     * @throws Throwable
     */
    public function edit(): void
    {
        $id  = $this->request->param($this->model->getPk());
        $row = $this->model->find($id);
        if (!$row) {
            $this->error(('Record not found'));
        }
        $dataLimitAdminIds = $this->getDataLimitAdminIds();
        if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
            $this->error(('You have no permission'));
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$data) {
                $this->error(('Parameter %s can not be empty'));
            }
            $data   = $this->excludeFields($data);
            $data['url'] = reset($data['banner_path']);
            $data['banner_path'] = json_encode($data['banner_path'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
           
            $result = false;
            $this->model->startTrans();
            try { 
                $result = $row->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(('Update successful'));
            } else {
                $this->error(('No rows updated'));
            }
        }
        if(isJson($row['banner_path']) && !empty($row['banner_path'])){
            $row['banner_path'] = json_decode($row['banner_path'],true);
        }

        $this->success('', [
            'row' => $row
        ]);
    }


    protected function getSelectList(): array
    {

        $res = Navigation::where(['pid'=>0,'status'=>1])->select()->toArray();
        $isSelect = [];
        foreach($res as $key=>$argv){
            $isSelect[$argv['id']] = $argv['title'];
        }
        return $isSelect;
    }



    


}